import {copyCoreDist, release, replaceFileContent, version, getAdditionData} from "ctool-adapter-base";
import {tools, ToolInterface, FeatureInterface, AllLocaleStructure} from "ctool-config";
import {CustomAction, customActions} from "./config";
import {join} from "path";
import {cpSync, mkdirSync, rmSync} from "fs";

const tempPath = join(__dirname, '../_temp')
rmSync(tempPath, {recursive: true, force: true});
mkdirSync(tempPath);

// 核心文件
copyCoreDist(tempPath)
// 平台文件
cpSync(join(__dirname, '../resources'), tempPath, {recursive: true})

const i18n: AllLocaleStructure = getAdditionData()['i18n']

const getToolTitle = (tool: ToolInterface) => {
    return i18n.detail.zh_CN[`tool_${tool.name}`]?.message || ""
}
const getToolFeatureTitle = (feature: FeatureInterface) => {
    return i18n.detail.zh_CN[`tool_${feature.tool.name}_${feature.name}`]?.message || ""
}

type focusanyAction = {
    name: string,
    title: string,
    matches: (string | CustomAction)[]
}

let focusanyAction: Record<string, any>[] = [{
    "name": "ctool",
    "title": "程序员工具箱",
    "matches": ['ctool', '程序员工具箱']
}];
tools.forEach(tool => {
    tool.features.forEach(feature => {
        const name: focusanyAction['name'] = `ctool-${tool.name}-${feature.name}`
        const title: focusanyAction['title'] = `${tool.isSimple() ? "" : getToolTitle(tool) + "-"}${getToolFeatureTitle(feature)}`
        const matches: focusanyAction['matches'] = []
        if (customActions.has(feature)) {
            matches.push(...(customActions.get(feature) as CustomAction[]).map((item => {
                // item.label = title
                return item
            })))
        }
        if (matches.length > 0) {
            focusanyAction.push({
                name,
                title,
                matches
            })
        }
    })
});

(async () => {
    // 写入版本号
    replaceFileContent(join(tempPath, 'config.json'), '##version##', version())
    replaceFileContent(join(tempPath, 'config.json'), '"##actions##"', JSON.stringify(focusanyAction))
    // 发布
    console.info(`focusany: ${await release(tempPath, 'focusany')}`)
    // 移除临时目录
    // rmSync(tempPath, {recursive: true, force: true});
})()
